package xiaohongshu.com.sunny.t1;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String listValues = scanner.nextLine();
        String[] values = listValues.split(" ");
        ListNode head = createList(values);
        int k = scanner.nextInt();
//        System.out.println(k);
        if(k == 1){
            print(head);
        }else{
            int len = getLen(head);
            int rest = len;
            int ind = 1;
            while (rest >= k){
//                System.out.println(ind+" "+ind+k);
                head = reverseBetween(head, ind, ind+k-1);
                ind = ind+k;
                rest -= k;
            }
            print(head);
        }

//        System.out.println(head);

    }

    public static int getLen(ListNode head){
        ListNode node = head;
        int res = 0;
        while (null != node){
            res++;
            node = node.next;
        }
        return res;
    }

    public static void print(ListNode head){
        ListNode node = head;
        while (null != node){
            System.out.print(node.val);
            if(node.next != null)
                System.out.print(" ");
            node = node.next;
        }
        System.out.println();
    }

    public static ListNode createList(String[] values){
        ListNode head = new ListNode(Integer.parseInt(values[0]));
        ListNode node = head;
        for(int i=1;i<values.length;i++){
            int tmp = Integer.parseInt(values[i]);
            ListNode tmpNode = new ListNode(tmp);
            node.next = tmpNode;
            node = node.next;
        }
        return head;
    }

    public static ListNode reverseBetween(ListNode head, int m, int n) {
        if(m == n)
            return head;
        if(1 == m){
            ListNode node = head;
            for(int i=0;i<n;i++){
                node = node.next;
            }
            ListNode p1 = null;
            ListNode p2 = head;
            ListNode p3 = head.next;
            while (node != p2){
                p2.next = p1;
                p1 = p2;
                p2 = p3;
                if(node != p3)
                    p3 = p3.next;
            }
            head.next = node;
            head = p1;
            return head;
        }else{
            ListNode nodeBack = null;
            ListNode nodeM = head;
            for(int i=0;i<m-1;i++) {
                nodeBack = nodeM;
                nodeM = nodeM.next;
            }
            ListNode nodeN = head;
            for(int i=0;i<n;i++)
                nodeN = nodeN.next;
            ListNode p1 = null;
            ListNode p2 = nodeM;
            ListNode p3 = nodeM.next;
            while (nodeN != p2){
                p2.next = p1;
                p1 = p2;
                p2 = p3;
                if(nodeN != p3){
                    p3 = p3.next;
                }
            }
            nodeBack.next = p1;
            nodeM.next = nodeN;
            return head;
        }
    }

}
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
        next = null;
    }

    @Override
    public String toString() {
        return "ListNode{" +
                "val=" + val +
                ", next=" + next +
                '}';
    }
}
